<?PHP
    require_once('ConnectionMysql.php');
    require_once('Usuario.php');

    class UsuarioDAO
    {
        private $connection = NULL;


        /*************************************************************************
        * Name: __construct
        * Description: Construtor da Classe. Inicializar os Atributos.
        * Parameters: 
        * Returns: Nenhum
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function __construct()
        {
            $this->connection = new ConnectionMysql(); // Cria a Conexão.
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function insert(Usuario $obj)
        {
           $sql = "INSERT INTO usuario (nome, email, emailAlternativo, senha, idTipoUsuario, linkCurriculo, isAtivo)
                    VALUES ('". $obj->getNome() ."', '". $obj->getEmail() ."', '". $obj->getEmailAlternativo() ."', '". $obj->getSenha() ."', ". $obj->getIdTipoUsuario() .", ". 
                   "'".$obj->getLinkCurriculo()."'".
                    //$obj->getLinkCurriculo() == NULL ? $obj->getLinkCurriculo() : "'".$obj->getLinkCurriculo()."'".
                    ", '". $obj->getIsAtivo() ."')";
            
            return $this->connection->queryConnect($sql);
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function deletePorId($id)
        {
            $sql = "UPDATE usuario SET isAtivo='N' WHERE idUsuario=". $id;
            $this->connection->queryConnect($sql);
        }
        
        
        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function deletePorObj(Usuario $obj)
        {
            $sql = "UPDATE usuario SET isAtivo='N' WHERE idUsuario=". $obj->getIdUsuario();
            $this->connection->queryConnect($sql);
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function update(Usuario $obj)
        {
            $sql = "UPDATE usuario 
                    SET nome='". $obj->getNome() ."', email='". $obj->getEmail() ."', emailAlternativo='". $obj->getEmailAlternativo() ."', senha='". $obj->getSenha() ."', idTipoUsuario=". $obj->getIdTipoUsuario() .", isAtivo='". $obj->getIsAtivo() .", 
                    linkCurriculo=". $obj->getLinkCurriculo() == NULL ? $obj->getLinkCurriculo() : "'".$obj->getLinkCurriculo()."'" ."
                    WHERE idUsuario=". $obj->getIdUsuario();
            $this->connection->queryConnect($sql);
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function selectAutenticar($email, $senha)
        {
            $sql = "SELECT idUsuario, nome, idTipoUsuario FROM usuario WHERE email='". $email ."' AND senha='". $senha ."' AND isAtivo='S'";
            $result = $this->connection->queryConnect($sql);
            
            if (mysql_num_rows($result) > 0)
            {
                $obj = new Usuario();
                $obj->setIdUsuario(mysql_result($result, 0, "idUsuario"));
                $obj->setNome(mysql_result($result, 0, "nome"));
                $obj->setIdTipoUsuario(mysql_result($result, 0, "idTipoUsuario"));
            
                return $obj;
            }
            else
            {
                return NULL;
            }
        }
        
        
        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function selectAllInfo($idUser)
        {
            $sql = "SELECT nome, email, emailAlternativo, senha, idTipoUsuario, linkCurriculo FROM usuario WHERE isAtivo='S' AND idUsuario=". $idUser;
            $result = $this->connection->queryConnect($sql);
            
            if (mysql_num_rows($result) > 0)
            {
                $obj = new Usuario();
                $obj->setIdUsuario(mysql_result($result, 0, $idUser));
                $obj->setNome(mysql_result($result, 0, "nome"));
                $obj->setEmail(mysql_result($result, 0, "email"));
                $obj->setEmailAlternativo(mysql_result($result, 0, "emailAlternativo"));
                $obj->setSenha(mysql_result($result, 0, "senha"));
                $obj->setIdTipoUsuario(mysql_result($result, 0, "idTipoUsuario"));
                $obj->setLinkCurriculo(mysql_result($result, 0, "linkCurriculo"));
            
                return $obj;
            }
            else
            {
                return NULL;
            }
        }
        
        
        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function select()
        {
            $sql = "";
            $this->connection->queryConnect($sql);
        }
    }
?>